package com.example.cardgame

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class FirstActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_first)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        val button = findViewById<TextView>(R.id.button)
        button.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivityForResult(intent,2)

        }
        val button2 = findViewById<TextView>(R.id.button2)
        button2.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent,1)
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                val textView = findViewById<TextView>(R.id.textView)
                val textView2 = findViewById<TextView>(R.id.textView2)
                val textView3 = findViewById<TextView>(R.id.textView3)
                val textView4 = findViewById<TextView>(R.id.textView4)
                val textView5 = findViewById<TextView>(R.id.textView5)
                val textView6 = findViewById<TextView>(R.id.textView6)
                textView.text = "姓名:" + data?.getStringExtra("name")
                textView2.text = "电子邮箱:" + data?.getStringExtra("Email_address")
                textView3.text = "电话号码:" + data?.getStringExtra("phone")
                textView4.text = "爱好:" + data?.getStringExtra("hobby")
                textView5.text = "学号:" + data?.getStringExtra("Sno")
                textView6.text = "性别:" + data?.getStringExtra("sex")
                val imageBitmap = data?.extras?.get("imageBitmap") as? Bitmap
                if (imageBitmap != null) {
                    val imageView = findViewById<ImageView>(R.id.imageView)
                    imageView.setImageBitmap(imageBitmap)
                }


            }
        }
        if(requestCode == 2){
            if(resultCode == RESULT_OK){
                val textView = findViewById<TextView>(R.id.textView9)
                textView.text = "最佳成绩:" + data?.getIntExtra("highestScore",0).toString()
            }
        }

    }
}

